/*
$(function () {
    vm.getInfo(getUrlParam("orderNo"));
});
/!**
 * 取得url参数
 *!/
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  // 匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; // 返回参数值
}
*/

var erpOrderLogisticVm = new Vue({
    el:'#erpOrderLogisticPage',
    data:{
        orderNo:'',
        erpOrder: {},
        title:'',
        //用于展示
        erpOrderItemData:[],
        //用于接收选中的
        erpLogisticsItemList:[],
        erpLogistic:{toAreaId:''},
        sellerInfo:{},
        logisticsTypeList:[],
        e:{
            LogisticsLogiTypeEnum:[],
            LogisticsSendTypeEnum:[],
        },
        showLogiNo:true,
        showSendDetail:true,
        area:[],
        erpOrderLogisticVisible:false,
        /* 省市区回显 */
        areaCascader: [],
        //添加出库对象
        erpLogisticsDto:{erpLogisticsEntity:{},erpLogisticsItemList:[]},
    },
    //处理物流单号特殊字符
    watch:{
        'erpLogistic.logiNo':function (val) {
            this.erpLogistic.logiNo = erpOrderLogisticVm.filterLogiNo(val);
        }
    },
    methods:{
        loadData:function(data){
            erpOrderLogisticVm.orderNo=data.orderNo;
            erpOrderLogisticVm.title=data.title;
            erpOrderLogisticVm.getInfo( erpOrderLogisticVm.orderNo);
        },
        getInfo:function (orderNo) {
            var that=this;
            //先根据订单号查询出库所需相关参数
            $.get(baseURL + "order/erporder/searchLogisticData/"+orderNo, function(r) {
                if(r.code==0){
                    erpOrderLogisticVm.erpOrder=r.erpOrder;
                    erpOrderLogisticVm.sellerInfo=r.sellerInfo;
                    erpOrderLogisticVm.logisticsTypeList=r.logisticsTypeList;
                    erpOrderLogisticVm.erpOrderItemData=r.erpOrderItemData;
                    erpOrderLogisticVm.area=JSON.parse(r.area);
                    //that.$refs.multipleTable.toggleAllSelection();
                    erpOrderLogisticVm.erpLogistic={
                        toPeople: erpOrderLogisticVm.erpOrder.shipName,
                        toPhone:  erpOrderLogisticVm.erpOrder.shipMobile == null ? "":erpOrderLogisticVm.erpOrder.shipMobile+""+(erpOrderLogisticVm.erpOrder.shipTel==null?"":","+erpOrderLogisticVm.erpOrder.shipTel),
                        toAddress: erpOrderLogisticVm.erpOrder.shipAddr,
                        orderNo: erpOrderLogisticVm.erpOrder.orderNo,
                        fromPeople: erpOrderLogisticVm.sellerInfo.sellerName,
                        fromPhone: erpOrderLogisticVm.sellerInfo.sellerMobile,
                        fromAddress :erpOrderLogisticVm.sellerInfo.addr,
                        toAreaId:erpOrderLogisticVm.erpOrder.shipAreaId,
                        sendType: 3,
                        logiType: 2,
                        toArea:erpOrderLogisticVm.erpOrder.shipArea,
                    }
                    //处理省市区回显
                    var regionPath = erpOrderLogisticVm.erpOrder.shipArea;
                    if(!isBlank(regionPath)){
                      var  arr=regionPath.split(/\s+/);
                      if(arr.length==3){
                          erpOrderLogisticVm.areaCascader= erpOrderLogisticVm.cascaderRet(arr,erpOrderLogisticVm.area);
                      }
                    }
                    erpOrderLogisticVm.erpOrderLogisticVisible=true;
                    Vue.nextTick(function () {
                        that.$refs.multipleTable.toggleAllSelection();
                    })
                }else{
                    alert(r.msg);
                    return;
                }
            })
        },
        //处理省市区回显,
        cascaderRet:function(arr,area){
            debugger
            var province='';
            var city='';
            var are='';
            var cityArray=[];
            var areaArray=[];
            var resArray=[];
            for (var i=0;i<area.length;i++){
                if(arr[0]==area[i].label){
                    province=area[i].value;
                    cityArray=area[i].children;
                    break;
                }
            }
            for (var i=0;i<cityArray.length;i++) {
                if(arr[1]==cityArray[i].label){
                    city=cityArray[i].value;
                    areaArray=cityArray[i].children;
                    break;
                }
            }
            for (var i=0;i<areaArray.length;i++) {
                if(arr[2]==areaArray[i].label){
                    are=areaArray[i].value;
                    break;
                }
            }
            resArray.push(province);
            resArray.push(city);
            resArray.push(are);
            return resArray;
        },
        orderItemSelectionChange:function(row){
            var list = new Array();
            for (var i = 0, erpOrderItem; erpOrderItem = row[i++];) {
                var logisticsItem = {
                    orderItemId: erpOrderItem.eoItemId,
                    orderItemName: erpOrderItem.name,
                    quantity: erpOrderItem.nums,
                }
                list.push(logisticsItem);
            }
            erpOrderLogisticVm.erpLogisticsItemList = list;
        },
        handleItemChange:function(val){
            var values = val.join(',').split(',');
            var areaId = values[val.length - 1];
            erpOrderLogisticVm.erpLogistic.toAreaId = areaId;
            var lableObj = getCascaderObj(val, erpOrderLogisticVm.area);
            var lable = lableObj.join(',').replace(new RegExp(',', 'g'), ' ');
            erpOrderLogisticVm.erpLogistic.toArea = lable;
        },
        sendTypeChange:function(value){
            if (value == 1) {
                erpOrderLogisticVm.showSendDetail = false;
            } else {
                erpOrderLogisticVm.showSendDetail = true;
            }
        },
        logiTypeChange:function(value){
            for (var i = 0, logisticsType; logisticsType = erpOrderLogisticVm.logisticsTypeList[i++];) {
                // 紫云物流 不填写配送单号
                if (logisticsType.logisticsId == value){
                    erpOrderLogisticVm.erpLogistic.logiCompany = logisticsType.logisticsName;
                    if (logisticsType.logisticsName.indexOf("紫云-") == 0){
                        erpOrderLogisticVm.showLogiNo = false;
                        break;
                    }
                }else {
                    erpOrderLogisticVm.showLogiNo = true;
                }
            }
        },
        // 保存物流信息
        saveLogistics:function () {
            // 验证参数
            if (!erpOrderLogisticVm.validator()) {
                return false;
            }
            // 封装数据给后台
            erpOrderLogisticVm.erpLogisticsDto.erpLogisticsEntity=erpOrderLogisticVm.erpLogistic;
            erpOrderLogisticVm.erpLogisticsDto.erpLogisticsItemList=erpOrderLogisticVm.erpLogisticsItemList;

            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/saveErpOrderLogistics",
                contentType: "application/json",
                data: JSON.stringify(erpOrderLogisticVm.erpLogisticsDto),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            erpOrderLogisticVm.reback();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        filterLogiNo:function(val){
            return val.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g, "");
        },

        // 参数验证
        validator: function () {
            if (erpOrderLogisticVm.erpLogistic.logiType != 3){
                if (isBlank(erpOrderLogisticVm.erpLogistic.logisticsId)) {
                    alert("物流方式不能为空");
                    return false;
                }
                if (isBlank(erpOrderLogisticVm.erpLogistic.toPhone)) {
                    alert("收货联系方式不能为空");
                    return false;
                }
                if (erpOrderLogisticVm.areaCascader.length!=3) {
                    alert(" 收货地区不能为空！");
                    return false;
                }
                if (isBlank(erpOrderLogisticVm.erpLogistic.toAddress)) {
                    alert("收货地址不能为空");
                    return false;
                }
            }
            if (isBlank(erpOrderLogisticVm.erpLogistic.toPeople)) {
                alert("收货人不能为空");
                return false;
            }
            return true;
        },
        reback:function () {
            erpOrderLogisticVm.erpOrderLogisticVisible=false;
           vm.reload();
         // window.location.hash =baseURL+"#modules/order/erporder.html";
            //window.location.hash = '/modules/order/erpOrder.html';
            //window.parent.vm.goto({hash:'#modules/order/erporder.html'})
        }
    }
});

/**
 * 省市区级联选择
 * @param val
 * @param opt
 */
function getCascaderObj(val, opt) {
    return val.map(function (value, index, array) {
        for (var itm in opt) {
            if (opt[itm].value == value) {
                var label = opt[itm].label;
                opt = opt[itm].children;
                return label;
            }
        }
        return null;
    });
}
